﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ATM
{
    public partial class chuyenkhoan : Form
    {
        private SqlConnection conn;
        private SqlCommand cmd,cmd1,cmd2,cmd3,cmd4;
        string s;
      
       
        public chuyenkhoan(string sodu)
        {
            InitializeComponent();
            label5.Text = sodu;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string con=ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn=new SqlConnection(con);
            cmd = new SqlCommand("select Acc_Balance from TB_ACCOUNT where Acc_No='" + label5.Text + "'", conn);
            conn.Open();

            SqlDataReader reader=cmd.ExecuteReader();
            reader.Read();
           s=reader.GetValue(0).ToString();
           textBox1.Text = s;
            conn.Close();
          
            
          
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text =textBox2.Text+ button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button5.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button10.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button11.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button12.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button13.Text;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("Chua nhap day du thong tin");
            }
            else
            {
                if (textBox3.Text == label5.Text)
                {

                    MessageBox.Show("Khong the chuyen cho chinh minh");
                }
                else
                {


                    cmd = new SqlCommand("select Acc_No from TB_ACCOUNT where Acc_No='" + textBox3.Text + "' ", conn);
                   conn.Open();
                    SqlDataReader rd = cmd.ExecuteReader();
                   
                        if (rd.Read())
                        {

                            conn.Close();
                            if (float.Parse(textBox1.Text) - float.Parse(textBox2.Text) < 0)
                            {
                                MessageBox.Show("Tai khoan cua ban khong du");

                                textBox2.Text = "";
                                textBox3.Text = "";
                            }
                            else
                            {
                                if (MessageBox.Show("Bạn có chắc chắn muốn chuyển không ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    textBox4.Text = (float.Parse(textBox1.Text) - float.Parse(textBox2.Text)).ToString();
                                    cmd1 = new SqlCommand("update TB_ACCOUNT set Acc_Balance='" + textBox4.Text + "' where Acc_No='" + label5.Text + "'", conn);
                                    cmd2 = new SqlCommand("update TB_ACCOUNT set Acc_Balance=Acc_Balance+'" + textBox2.Text + "' where Acc_No='" + textBox3.Text + "'", conn);
                                    conn.Open();
                                    cmd1.ExecuteNonQuery();
                                    cmd2.ExecuteNonQuery();
                                    baocao();
                                    string s4 = DateTime.Now.ToShortDateString();
                                    conn.Close();
                                    if (MessageBox.Show("Giao dịch thành công.Bạn có muốn lấy hóa đơn không?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                    {
                                        hoadon2 f = new hoadon2(label5.Text, textBox3.Text, textBox2.Text, textBox1.Text,s4);
                                        f.Show();
                                        this.Hide();

                                    }
                                }
                            }
                        


                    }
                    else 
                    {
                        MessageBox.Show("Tai khoan khong ton tai");
                        this.Hide();
                        if (MessageBox.Show("Bạn có giao dịch nữa không ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            chuyenkhoan f = new chuyenkhoan(label5.Text);
                            f.Show();

                        }
                        else {
                            main f = new main(label5.Text);
                            f.Show();
                        }
                    }
                }
            }
        }
                   
                   
                
                                
            
        
        public void baocao() {


            cmd3 = new SqlCommand("select Acc_ID from TB_ACCOUNT where  Acc_No='" + label5.Text + "'  ", conn);
        
            SqlDataReader rd1 = cmd3.ExecuteReader();
            rd1.Read();
            string s1=rd1.GetValue(0).ToString();
            conn.Close();
            cmd4 = new SqlCommand("select Acc_ID from TB_ACCOUNT where  Acc_No='" + textBox3.Text + "'  ", conn);

            conn.Open();
            SqlDataReader rd2 = cmd4.ExecuteReader();
            rd2.Read();
            string s2=rd2.GetValue(0).ToString();
            conn.Close();
            string s3 = DateTime.Now.ToShortDateString();
            cmd = new SqlCommand("insert into TB_TRANSACTION(Tran_Type,Acc_Pri_ID,Acc_sec_ID,Tran_Amount,Tran_Balance,Tran_Date) values (2,'"+s1+"','"+s2+"','"+textBox2.Text+"','"+textBox4.Text+"','"+s3+"')", conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            string tk = label5.Text;
            string tk1 = textBox3.Text;
            string tienchuyen = textBox2.Text;

            string sodu = textBox4.Text;
            string ngay = DateTime.Now.ToShortDateString();
            hoadon2 f = new hoadon2(tk,tk1,tienchuyen,sodu,ngay);
            f.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            main f=new main(label5.Text);
            f.Show();
            this.Hide();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }
    }
}
